import SwiftUI
import SPGlassKit

public struct GlassMapPinExample: GlassExample {
    public static let componentID = "glass-map-pin"

    private struct Place: Identifiable {
        let id: String
        let title: String
        let content: GlassMapPinContent
        let tint: GlassColorToken
        /// Position in the unit square of the placeholder map.
        let position: UnitPoint
    }

    /// Fictitious places.
    private let places = [
        Place(id: "cafe", title: "Café Exemple", content: .symbol("cup.and.saucer.fill"), tint: .primary, position: UnitPoint(x: 0.25, y: 0.3)),
        Place(id: "museum", title: "Musée fictif des très longs noms de lieux", content: .symbol("building.columns.fill"), tint: .accent, position: UnitPoint(x: 0.7, y: 0.25)),
        Place(id: "price", title: "Listing A-12", content: .text("$1.2M"), tint: .success, position: UnitPoint(x: 0.5, y: 0.55)),
        Place(id: "warning", title: "Road works", content: .symbol("exclamationmark.triangle.fill"), tint: .warning, position: UnitPoint(x: 0.2, y: 0.75)),
    ]
    @State private var selection: String? = "cafe"

    public init() {}

    public var body: some View {
        GeometryReader { proxy in
            SampleMapBackdrop()
                .overlay {
                    ForEach(places) { place in
                        Button {
                            selection = selection == place.id ? nil : place.id
                        } label: {
                            GlassMapPin(place.content, title: place.title, tint: place.tint, isSelected: selection == place.id)
                        }
                        .buttonStyle(.plain)
                        .position(x: proxy.size.width * place.position.x, y: proxy.size.height * place.position.y)
                    }
                    // Cluster markers: the count comes from the map engine, the pin only formats it.
                    GlassMapPin(clusterCount: 12)
                        .position(x: proxy.size.width * 0.8, y: proxy.size.height * 0.6)
                    GlassMapPin(clusterCount: 1_340, title: "Downtown")
                        .position(x: proxy.size.width * 0.75, y: proxy.size.height * 0.8)
                }
        }
    }
}

#Preview("GlassMapPin") { GlassMapPinExample().glassTheme(.solar) }
